package task.manager;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NewCategory extends Activity {
	
	private Button btnSubmit;
	private Button btnCancel;
	
	private EditText etCategory;
	
	private TextView lblConfirmation;
	
	private CategoryFacade categoryFacade;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newcategory);
        
        categoryFacade = CategoryFacade.getInstance();
        
        btnSubmit = (Button)findViewById(R.id.button1);
        btnCancel = (Button)findViewById(R.id.button2);
        etCategory = (EditText)findViewById(R.id.editText1);
        lblConfirmation = (TextView)findViewById(R.id.textView2);
        System.out.println("lblConfirmation: " + lblConfirmation);
        
        btnSubmit.setOnClickListener(new OnClickListener() {     	
        	public void onClick(View v) {
        		
        		// Variables to check username and password.
        		String cat = etCategory.getText().toString();
        		
        		if(cat.length() > 0){ // If the user has entered a username.
        				categoryFacade.addNewCategory(new Category(cat));
        				Intent nextScreen = new Intent(getApplicationContext(), CategoryViewActivity.class);
                		startActivity(nextScreen);
                		finish();
        		} // End of if statement.
        		else{ // Else the user has not entered a username.
        			//lblConfirmation.setText("Please enter a category");
        		} // End of else statement.
        		
        		
        		
        	} // End of onClick method.
        }); // End of setOnClickListener event.
        
        /** 
         * Binding Click event to cancel button 
         */
        btnCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Closes activity.
            	Intent nextScreen = new Intent(getApplicationContext(), CategoryViewActivity.class);
        		startActivity(nextScreen);
            	finish();
            } // End of onClick method.
        }); // End of setOnClickListener event.
        
	} // End of onCreate method.
	
	@Override
	public void onPause() {
		super.onPause();
		try {
			categoryFacade.saveCategories();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

